<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 地图区域划分 公共方法类
 * @package  htdocs
 * @subpackage  module/base
 * @author   create by allen.qiang
 * @version 1.0
 */
class mapAreaZoning {

    /**
     * 公共的修改添加地图区域名称方法
     * @param array $arg_array
     * @return array
     */
    public function editMapArea( $arg_array ) {

        global $G_SHOP;

        quanli_require('module/base/system/sysRegion.class.php');

        if(!isset($arg_array['areaCode'], $arg_array['areaName'], $arg_array['color'], $arg_array['cityID'], $arg_array['type'],$arg_array['operator'], $arg_array['path'][0]) ){
            return array('code'=>400,'message'=>'缺少参数');
        }

        if (!quanli_is_int($arg_array['cityID'])) {
            return array('code'=>400,'message'=>'参数错误');
        }

        if (empty($arg_array['areaName'])) {
            return array( 'code' => 400, 'message' => '区域名称不能为空' );
        }

        if (empty($arg_array['areaCode'])) {
            return array( 'code' => 400, 'message' => '区域编码不能为空' );
        }

        if (empty($arg_array['color'])) {
            return array( 'code' => 400, 'message' => '请选择颜色' );
        }

        if (!in_array($arg_array['type'], array(1, 2))) {
            return array( 'code' => 400, 'message' => '请选择类型' );
        }

        if (!isset($arg_array['areaID']) || !quanli_is_int($arg_array['areaID'])) {
            $areaID = 0;
        } else {
            $areaID = $arg_array['areaID'];
        }

        $sysRegion = new sysRegion( $G_SHOP->DBCA );

        try {

            //开启事务
            $G_SHOP->DBCA->beginTransaction();

            //查询城市信息
            $region_args_array = array(
                'id' => $arg_array['cityID'],
                'region_level' => G_CITY_LEVEL
            );
            $region_array = $sysRegion->getAreaManageList( $region_args_array );
            if (!$region_array) {
                throw new Exception('城市信息错误', 404);
            }


            $sys_map_area = new entity('sys_map_area');
            if ($areaID > 0) {
                $sys_map_area->areaID = $areaID;
                if (!$G_SHOP->DBCA->fetch($sys_map_area)) {
                    throw new Exception('区域信息没有找到', 404);
                }

                $G_SHOP->DBCA->stmt_query('DELETE FROM sys_map_area_detail WHERE areaID = ?', 'i', $areaID);

            } else {
                $sys_map_area->creator = $arg_array['operator'];
            }

            $sys_map_area->areaCode = $arg_array['areaCode'];
            $sys_map_area->areaName = $arg_array['areaName'];
            $sys_map_area->colour = $arg_array['color'];
            $sys_map_area->cityID = $region_array[0]['id'];
            $sys_map_area->cityName = $region_array[0]['name'];
            $sys_map_area->type = $arg_array['type'];
            $G_SHOP->DBCA->save($sys_map_area);


            if ($areaID == 0) {
                $areaID = $G_SHOP->DBCA->insert_id;
            }

            //保存区域详情数据
            foreach($arg_array['path'] as $v){
                $sys_map_area_detail = new entity('sys_map_area_detail');
                $sys_map_area_detail->areaID = $areaID;
                $sys_map_area_detail->creator = $arg_array['operator'];
                $sys_map_area_detail->longitude = $v['lng'];
                $sys_map_area_detail->latitude  = $v['lat'];
                $G_SHOP->DBCA->save($sys_map_area_detail);
            }


            $G_SHOP->DBCA->stmt_query('DELETE FROM sys_map_area_brand WHERE cityID = ? and areaID=? and type=?', 'iii', array($arg_array['cityID'],$areaID,$arg_array['type']));

            //绑定品牌 sunqiang
            if(isset($arg_array['brand']))
            {

                foreach($arg_array['brand'] as $brand_id)
                {
                    $map_area_brand = new entity('sys_map_area_brand');
                    $map_area_brand->brandID = $brand_id;
                    $map_area_brand->cityID = $arg_array['cityID'];
                    $map_area_brand->areaID = $areaID;
                    $map_area_brand->type = $arg_array['type'];
                    $G_SHOP->DBCA->save($map_area_brand);
                }

            }



            //提交事务
            $G_SHOP->DBCA->commit();

            return array( 'code'=>200, 'message'=>'保存成功', 'data'=> array( 'areaID' => $areaID ) ) ;

        } catch (Exception $e) {

            //回滚事务
            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    /**
     * 删除地址区域
     * @param int $areaID 地图区域ID
     * @return array
     */
    public function delMapArea( $areaID ) {

        global $G_SHOP;

        if (!isset($areaID) || !quanli_is_int($areaID)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            //开启事务
            $G_SHOP->DBCA->beginTransaction();

            //删除地图区域
            $sys_map_area = new entity('sys_map_area');
            $sys_map_area->areaID = $areaID;
            if (!$G_SHOP->DBCA->fetch($sys_map_area)) {
                throw new Exception('区域信息没有找到', 404);
            }
            $G_SHOP->DBCA->delete($sys_map_area);

            //删除地图区域详情
            $G_SHOP->DBCA->stmt_query('DELETE FROM sys_map_area_detail WHERE areaID = ?', 'i', $areaID);

            //提交事务
            $G_SHOP->DBCA->commit();

            return array( 'code' => 200, 'message'=>'地图区域删除成功' ) ;

        } catch (Exception $e) {

            //回滚事务
            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    /**
     * 查询地图区域所在的经纬度集合
     * @param int $areaID  地图区域ID
     * @return null|array
     */
    public function searchMapAreaPath( $areaID ) {

        if (!isset($areaID) || !quanli_is_int($areaID)) {
            return null;
        }

        global $G_SHOP;

        //查询地图区域详情信息
        $search_sql = 'SELECT * FROM sys_map_area_detail WHERE areaID = ?';
        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, 'i', $areaID);
        if (!$temp_item_array)
            return null;

        $item_array = array();
        foreach($temp_item_array as &$item) {
            $item_array[] = array(
                'areaID' => $item->areaID ,
                'longitude'=>$item->longitude,
                'latitude'=>$item->latitude
            );
        }

        return $item_array;

    }

    /**
     *查询地图区域关联的品牌
     *
     */
    public function searchMapAreaBrand($areaID,$cityID,$typeID)
    {
        if (!isset($areaID) || !quanli_is_int($areaID) || !isset($cityID) || !quanli_is_int($cityID) || !isset($typeID) || !quanli_is_int($typeID)) {
            return null;
        }
        global $G_SHOP;

        //查询地图区域详情信息
        $search_sql = 'SELECT * FROM sys_map_area_brand WHERE areaID = ? and cityID = ? and type = ?';
        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, 'iii', array( $areaID , $cityID , $typeID));
        if (!$temp_item_array)
            return null;

        $brand_array = array();
        foreach($temp_item_array as &$item) {
            $brand_array[] = array(
                'brandID' => $item->brandID ,
                'cityID'=> $item->cityID,
                'areaID'=> $item->areaID,
                'type' => $item->type
            );
        }

        return $brand_array;
    }



    /**
     * 查询地图区域划分信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchMapArea( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //区域编号
        if (isset($arg_array['areaID']) && is_numeric($arg_array['areaID'])) {
            $sql .= ' AND areaID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['areaID'];
        }

        //区域名称
        if (isset($arg_array['areaName']) && !empty($arg_array['areaName'])) {
            $sql .= ' AND areaName like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['areaName'].'%';
        }

        //区域编码
        if (isset($arg_array['areaCode']) && !empty($arg_array['areaCode'])) {
            $sql .= ' AND areaCode = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['areaCode'];
        }

        //城市ID
        if (isset($arg_array['cityID']) && quanli_is_int($arg_array['cityID'])) {
            $sql .= ' AND cityID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityID'];
        }

        //类型
        if (isset($arg_array['type']) && is_numeric($arg_array['type'])) {
            $sql .= ' AND type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['type'];
        }

        //使用状态
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //品牌
        if (isset($arg_array['brandID']) && is_numeric($arg_array['brandID'] )) {
            $sql .= ' AND areaID in ( SELECT areaID FROM sys_map_area_brand WHERE brandID=? )';
            $params_type .= 'i';
            $params_array[] = $arg_array['brandID'];
        }




        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

            //统计数量
            $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_map_area ' . $sql;
            $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            $item_count = $count_array[0]->item_count;
            if ($item_count == 0) {
                $arg_array['count'] = 0;
                return null;
            }

            $arg_array['count'] = $item_count;

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            //分页
            if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                $arg_array['page'] = 1;

            $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

        } else {

            //排序
            if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

        }

        //查询分类信息
        $search_sql = 'SELECT * FROM sys_map_area ' . $sql;
        $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
        if (!$temp_item_array)
            return null;

        $item_array = array();
        foreach($temp_item_array as &$item) {
            $item_array[] = array(
                'creator' => $item->creator,
                'areaID' => $item->areaID ,
                'areaName' => $item->areaName,
                'areaCode' => $item->areaCode,
                'cityID' => $item->cityID,
                'cityName' => $item->cityName,
                'type' => $item->type,
                'colour'=>$item->colour,
                'useFlg' => 1
            );
        }

        return $item_array;

    }

    /**
     * 查询单个地图区域详情
     * @param int $areaID 地图区域ID
     * @return null|array
     */
    public function searchMapAreaDetail( $areaID ) {

        if (!isset($areaID) || !quanli_is_int($areaID)) {
            return null;
        }

        $arg_array = array(
            'areaID' => $areaID
        );
        return $this->searchMapArea( $arg_array );

    }

}
